package com.topgay.controller.admin;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.topgay.domain.Member;
import com.topgay.domain.ValueStrings;
import com.topgay.service.member_management.MemberService;

@Controller
@RequestMapping("/admin/")
public class AdminController {

	@Autowired
	private MemberService service;
	
	@RequestMapping("/login/main.do")
	public String mainPage(){
		return "admin/main";
	}
	
	//로그인
	@RequestMapping(value="/login.do", method=RequestMethod.GET)
	public String loginForm(){
		return "/admin/body/login.jsp";
	}
	
	//로그인 처리
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	public String login(String id, String password, HttpSession session, ModelMap map) throws SQLException{
		Member member = service.getMemberById(id);
		
		if(member!=null){
			if(password!=null && password.trim().length()!=0){
				if(password.equals(member.getPassword())){
					//로그인 성공
//					System.out.println(member);
					session.setAttribute("login_admin_info", member);
					return "admin/main";
				}else{
					//로그인 실패 : 비번 일치안함
					map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPw());
				}
			} else {
				//비밀번호 틀린 경우
				map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPwEmpty());
			}
		} else {
			//ID가 틀린경우, DB에 회원정보값 없음
			map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginId());
			
		}
		return "/admin/body/login.jsp";
	}
	
	@RequestMapping(value="/logout.do")
	public String logout(HttpSession session) throws IOException{
		session.invalidate();
//		return "admin/main";
		return "redirect:/admin/logout_refresh.html";
	}
	
	//회원정보
	@RequestMapping("/login/member_info.do")
	public String memberInfo(){
		return "admin/member_info";
	}
	
	//관리자정보
	@RequestMapping("/login/member_admin_info.do")
	public String adminInfo(){
		return "admin/member_admin_info";
	}
	
	//CS Center - 공지사항
	@RequestMapping("/login/cscenter_notice.do")
	public String cscenterNotice(){
		return "admin/cscenter_notice";
	}

	//CS Center - FAQ
	@RequestMapping("/login/cscenter_faq.do")
	public String cscenterFaq(){
		return "admin/cscenter_faq";
	}

	//CS Center - 1:1문의
	@RequestMapping("/login/cscenter_qna.do")
	public String cscenterQna(){
		return "admin/cscenter_qna";
	}

	//CS Center - 신고하기
	@RequestMapping("/login/cscenter_declare.do")
	public String cscenterDeclare(){
		return "admin/cscenter_declare";
	}

	//게시판 - 자유게시판
	@RequestMapping("/login/community_free.do")
	public String communityFree(){
		return "admin/community_free";
	}

	//게시판 - 팁게시판
	@RequestMapping("/login/community_tip.do")
	public String communityTip(){
		return "admin/community_tip";
	}

	//게시판 - 핫이슈
	@RequestMapping("/login/community_news.do")
	public String communityNews(){
		return "admin/community_news";
	}

	//게시판 - 고충상담
	@RequestMapping("/login/community_counseling.do")
	public String communityCounseling(){
		return "admin/community_counseling";
	}

}
